//
// Created by TATO on 2025/7/23.
//
#include "conf.h"
#include "http_socket.h"

int initconf(conf_t *global_conft, char* filename){
    int ret = 0;
    dts context = dtsnew("");
    char workfile[FILENAME_MAX];
    dts file = isEmpty(filename)?dtsnew(CONF_FILE):dtsnew(filename);
    if (strncmp(file, "/", 1) != 0) {
        if (strncmp(file, "./", 2) == 0) {
            dtsrange(file, 2, -2);
        }
        getcwd(workfile, FILENAME_MAX);
        file = dtscatsds(dtscat(dtsnew(workfile), "/"), file);
    }

    int fd = open(file, O_RDONLY);
    if (fd < 0) {
        return FALSE;
    }
    while (1) {
        char buf[BUF_SIZE];
        int len = Read(global_conft, fd, buf, sizeof buf);
        if(len > 0) {
            context = dtscat(context, buf);
        }else if(len == 0){
            break;
        }
    }
    if(ret = analysisconf(global_conft, context) < 0){
        return ret;
    }
    return ret;
}

int analysisconf(conf_t *global_conft, dts context_str)
{
    json* context_obj = json_Parse(context_str);
    if (context_obj == NULL) {
        const char *error_ptr = json_GetErrorPtr();
        if (error_ptr != NULL) {
            return -1;
        }
    } else {
        json* error_log = json_GetObjectItemCaseSensitive(context_obj, "error_log");
        json* success_log = json_GetObjectItemCaseSensitive(context_obj, "success_log");
        json* http_obj = json_GetObjectItemCaseSensitive(context_obj, "http");
        global_conft->gb_conf.error_log = dtsnew(error_log->valuestring);
        global_conft->gb_conf.success_log = dtsnew(success_log->valuestring);
        json* include_mime = json_GetObjectItemCaseSensitive(http_obj, "include_mime");
        json* default_mime = json_GetObjectItemCaseSensitive(http_obj, "default_mime");
        json* servers_obj = json_GetObjectItemCaseSensitive(http_obj, "servers");
        global_conft->gb_conf.http_conf.include_mime = dtsnew(include_mime->valuestring);
        global_conft->gb_conf.http_conf.default_mime = dtsnew(default_mime->valuestring);
        global_conft->gb_conf.http_conf.server_num = json_GetArraySize(servers_obj);
        json *server_obj, *proxy_obj;
        int i = 0;
        if (global_conft->gb_conf.http_conf.server_num > 0) {
            json_ArrayForEach(server_obj, servers_obj) {
                global_conft->gb_conf.http_conf.servers[i] = (servers_t *)http_malloc(sizeof (servers_t));
                json* listen = json_GetObjectItemCaseSensitive(server_obj, "listen");
                json* server_name = json_GetObjectItemCaseSensitive(server_obj, "server_name");
                global_conft->gb_conf.http_conf.servers[i]->listen = listen->valueint;
                global_conft->gb_conf.http_conf.servers[i]->server_name = dtsnew(server_name->valuestring);
                global_conft->gb_conf.http_conf.servers[i]->proxy_info = (map *)http_malloc(sizeof(map));
                initLeader(global_conft->gb_conf.http_conf.servers[i]->proxy_info);
                json* proxy_info = json_GetObjectItemCaseSensitive(server_obj, "proxy_info");
//                proxy_obj = proxy_info->child;
//                while(proxy_obj->next != NULL) {
//                    proxy_obj = proxy_obj->next;
//
//                }
                i++;
            }
        }
    }
    json_Delete(context_obj);
    return 0;
}